package me.victor.jetpackcompose

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter
import coil.decode.SvgDecoder
import coil.request.ImageRequest
import coil.transform.CircleCropTransformation
import jp.wasabeef.transformers.coil.BlurTransformation
import jp.wasabeef.transformers.coil.GrayscaleTransformation

/**
 * @author song.zheng
 * @create 2024/3/14
 * @description CoilImageLoadingLibrary
 */
@Composable
fun CoilImage() {
    Box(
        modifier = Modifier
            .width(150.dp)
            .height(150.dp),
        contentAlignment = Alignment.Center
    ) {
        val painter = rememberAsyncImagePainter(
            model = ImageRequest.Builder(LocalContext.current)
                .data("https://avatars.githubusercontent.com/u/13409222?s=200&v=4")
                .decoderFactory(SvgDecoder.Factory())
                .transformations(
                    GrayscaleTransformation(),
                    BlurTransformation(LocalContext.current),
                    CircleCropTransformation()
                )
                .crossfade(300)
                .build(), placeholder = painterResource(id = R.drawable.ic_google_logo)
        )
        val painterState = painter.state

        Image(
            painter = painter,
            contentDescription = "Logo Image",
            modifier = Modifier.fillMaxSize()
        )
        if (painterState is AsyncImagePainter.State.Loading) {
            CircularProgressIndicator()
        }
    }
}

@Composable
fun CoilSvg() {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        val painter = rememberAsyncImagePainter(
            model = ImageRequest.Builder(LocalContext.current)
                .data("http://www.clker.com/cliparts/u/Z/2/b/a/6/android-toy-h.svg")
                .decoderFactory(SvgDecoder.Factory())
                .build()
        )

        Image(
            painter = painter,
            contentDescription = "Svg",
            modifier = Modifier
                .width(180.dp)
                .height(300.dp)
        )
//        var atEnd by remember { mutableStateOf(false) }
//        val drawable = AnimatedImageVector.animatedVectorResource(R.drawable.ic_hourglass_animated)
//
//        IconButton(onClick = { atEnd = !atEnd }) {
//            Icon(
//                painter = rememberAnimatedVectorPainter(animatedImageVector = drawable, atEnd = atEnd),
//                contentDescription = "Animated Svg"
//            )
//        }
    }
}